//
//  BottomBarViewController.m
//  WisdomZhuHai
//
//  Created by JasonLee on 15/1/27.
//  Copyright (c) 2015年 wyd. All rights reserved.
//
//资讯详细内容view中的底部条

#import "BottomBarViewController.h"
#import "ProtocolManager.h"
#import "Common.h"
#import "TransforController.h"
#import "AppShareView.h"
#import "CommentViewController.h"
#import "UMSocial.h"
#import "LoginViewController.h"

@interface BottomBarViewController ()<UIAlertViewDelegate, UMSocialUIDelegate>

@property (nonatomic, strong) UIView *view_bottomBar;
@property (nonatomic, strong) NSArray *array_images;
@property (nonatomic, strong) NSDictionary *dic_data;
@property (nonatomic, assign) int isClickable;
@property (nonatomic, assign) BOOL isLiked;
@property (nonatomic, assign) BOOL isCollected;
@property (nonatomic, strong) NSString *string_share;

@end

@implementation BottomBarViewController


- (id)initWithFrame:(CGRect)frame{
    
    [super initWithNibName:nil bundle:nil];
    [self.view setFrame:frame];
    [self initBottomBarView];
//    self.array_images = [NSArray arrayWithArray:images];
    self.isLiked = false;
    self.isCollected = false;
    self.isClickable = 0;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void) initBottomBarView{
    self.array_images = @[[UIImage imageNamed:@"Guide/分享图标.png"], [UIImage imageNamed:@"Guide/收藏图标.png"], [UIImage imageNamed:@"Guide/评论图标.png"],[UIImage imageNamed:@"Guide/点赞图标.png"]];
    self.view_bottomBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
    self.view_bottomBar.backgroundColor = [TransforController getColorWithRed:210 andGreen:210 andBlue:210 andAlpha:1];
    [self.view addSubview:self.view_bottomBar];
    for(int i = 0; i < 4; i++){
        UIImageView *img_temp = [[UIImageView alloc]initWithFrame:CGRectMake(i * (320 / 4) + 25, 8, 25, 25)];
        UIButton *btn_temp = [[UIButton alloc]initWithFrame:CGRectMake(i * (320 / 4), 5, (320 / 4), 45)];
        img_temp.image = [self.array_images objectAtIndex:i];
        [img_temp setContentMode:UIViewContentModeScaleToFill];
        [img_temp setTag:300000 + i];
        [btn_temp setTag:200000 + i];
        [btn_temp addTarget:self action:@selector(doBtnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view_bottomBar addSubview:img_temp];
        [self.view_bottomBar addSubview:btn_temp];
    }
}

- (void)setEnabledWithData:(NSDictionary *)data{
    self.dic_data = [NSDictionary dictionaryWithDictionary:data];
    if(self.dic_data == nil || [[Common valueForKey:@"id" dic:self.dic_data] intValue] == 0){
        self.isClickable = 0;
    }
    NSLog(@"%s || string : %@", __FUNCTION__, [ProtocolManager shareInstance].loginName);
    if([ProtocolManager shareInstance].loginName == nil || [[ProtocolManager shareInstance].loginName isEqualToString:@""] || [[ProtocolManager shareInstance].loginName length] < 1){
        self.isClickable = 1;
        
    }else{
        self.isClickable = 2;
    }
    NSLog(@"%s || Actived's been actived with data:%@ and current status is:%d", __FUNCTION__, data, self.isClickable);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)doBtnClickAction:(id)sender{
    if(self.isClickable == 0){
        [[Common shareInstance] showAlertViewWithTitle:nil text:@"无法操作" dismissTime:0.8];
        return;
    }
//    } else if(self.isClickable == 1){
//        [[Common shareInstance] showAlertViewWithTitle:nil text:@"请先登录" dismissTime:0.8];
//        return;
//    }
    UIButton *btn_temp = sender;
    NSLog(@"%s || btn's tag:%ld", __FUNCTION__, (long)btn_temp.tag);
    switch(btn_temp.tag){
        case 200000:{
            if([[Common valueForKey:@"type" dic:self.dic_data]isEqualToString:@"NEWS"]){
                self.string_share = [Common valueForKey:@"url" dic:self.dic_data];
            }else{
                self.string_share = @"欢迎使用OK珠海,  享受“食、住、行、游、娱、购”移动端的“一站式”服务, 下载请戳 http://m.okhaolvxing.com/";
            }
            [self shareClick];
        }
            break;
        case 200001:{
            if(self.isClickable == 1){
                [self gotoLoginView];
//                return;
            }else if(self.isClickable == 2){
                if(self.isCollected){
                    UIAlertView *alert_temp = [[UIAlertView alloc]initWithTitle:@"取消收藏" message:@"您已收藏过，是否取消收藏？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                    [alert_temp show];
                }else{
                    [[ProtocolManager shareInstance]addToCollectionWithType:[Common valueForKey:@"type" dic:self.dic_data] andUsername:[ProtocolManager shareInstance].loginName andTargetId:[[Common valueForKey:@"id" dic:self.dic_data] intValue] andTarget:self andSelector:@selector(getCollectionCallBack:)];
                }
            }
        }
            break;
        case 200002:{
            if(self.isClickable == 1){
                [self gotoLoginView];
//                return;
            }else{
            NSLog(@"%s || goto commentList.", __FUNCTION__);
            [self gotoCommentList];
            }
        }
            break;
        case 200003:{
            if([[Common valueForKey:@"type" dic:self.dic_data] isEqualToString:@"GOODS"]){
                [[Common shareInstance] showAlertViewWithTitle:nil text:@"商品无法点赞" dismissTime:0.8];
                return;
            }
            if([[Common valueForKey:@"type" dic:self.dic_data]isEqualToString:@"STRATEGY"]){
                [[Common shareInstance] showAlertViewWithTitle:nil text:@"攻略无法点赞" dismissTime:0.8];
                return;
            }
            if(self.isLiked){
                [[Common shareInstance] showAlertViewWithTitle:nil text:@"您已点过赞了！" dismissTime:0.8];
                return;
            }
            NSLog(@"%s || dic_dataOfTop:%@, count:%d", __FUNCTION__, self.dic_data, [self.dic_data count]);
            [[ProtocolManager shareInstance]likeWithTypeName:[Common valueForKey:@"type" dic:self.dic_data] andTargetId:[[Common valueForKey:@"id" dic:self.dic_data] intValue] andTarget:self andSelector:@selector(getLikeCallBack:)];
        }
            break;
        default:
            break;
    }
}

-(void)shareClick
{
    [UMSocialSnsService presentSnsIconSheetView:[UIApplication sharedApplication].keyWindow.rootViewController
                                         appKey:nil
                                      shareText:self.string_share
                                     shareImage:[UIImage imageNamed:@"120.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToQQ,UMShareToSms,UMShareToWechatSession,UMShareToWechatTimeline,nil]
                                       delegate:self];
}

- (void)getLikeCallBack:(NSDictionary *)dic{
    NSLog(@"%s || dic: %@", __FUNCTION__, dic);
    if(dic == nil){
        return;
    }
    if([[Common valueForKey:@"respCode" dic:dic]isEqualToString:@"000"]){
        [[Common shareInstance] showAlertViewWithTitle:nil text:@"点赞成功" dismissTime:0.8];
        self.isLiked = true;
        UIImageView *img_temp = (UIImageView *)[self.view_bottomBar viewWithTag:300003];
        [img_temp setImage:[UIImage imageNamed:@"Guide/点赞图标clicked.png"]];
    }else{
        [[Common shareInstance] showAlertViewWithTitle:nil text:@"网络超时" dismissTime:0.8];
    }
    [TransforController umengEvent:@"UserLikeSuccess" attributes:@{@"Type" : [Common valueForKey:@"type" dic:self.dic_data], @"target_id":[Common valueForKey:@"id" dic:self.dic_data]} number:@(1) ct:@"900006"];
}

- (void)getCollectionCallBack:(NSDictionary *)dic{
    NSLog(@"%s || dic: %@", __FUNCTION__, dic);
    if(dic == nil){
        return;
    }
    if([[Common valueForKey:@"respCode" dic:dic]isEqualToString:@"000"]){
        [[Common shareInstance] showAlertViewWithTitle:nil text:@"收藏成功" dismissTime:0.8];
        self.isCollected = true;
        UIImageView *img_temp = (UIImageView *)[self.view_bottomBar viewWithTag:300001];
        [img_temp setImage:[UIImage imageNamed:@"Guide/收藏图标clicked.png"]];
    } else if([[Common valueForKey:@"respCode" dic:dic]isEqualToString:@"998"]){
        self.isCollected = true;
        UIImageView *img_temp = (UIImageView *)[self.view_bottomBar viewWithTag:300001];
        [img_temp setImage:[UIImage imageNamed:@"Guide/收藏图标clicked.png"]];
    } else {
        [[Common shareInstance] showAlertViewWithTitle:nil text:@"网络超时" dismissTime:0.8];
    }
    [TransforController umengEvent:@"UserCollectSuccess" attributes:@{@"Type" : [Common valueForKey:@"type" dic:self.dic_data], @"target_id":[Common valueForKey:@"id" dic:self.dic_data]} number:@(1) ct:@"900005"];
}

- (void)gotoLoginView{
    LoginViewController *loginview = [[LoginViewController alloc]init];
    loginview.hidesBottomBarWhenPushed = YES;
    [self.bottomBarNavigationController pushViewController:loginview animated:YES];
}

- (void)gotoCommentList{
    CommentViewController *commentViewController = [[CommentViewController alloc]init];
    commentViewController.hidesBottomBarWhenPushed = YES;
    commentViewController.target_id = [[Common valueForKey:@"id" dic:self.dic_data] intValue];
    commentViewController.type = [Common valueForKey:@"type" dic:self.dic_data];
    [self.bottomBarNavigationController pushViewController:commentViewController animated:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 1){
        [[ProtocolManager shareInstance]deleteFromCollectionWithType:[Common valueForKey:@"type" dic:self.dic_data] andUsername:[ProtocolManager shareInstance].loginName andTargetId:[[Common valueForKey:@"id" dic:self.dic_data] intValue] andTarget:self andSelector:@selector(getDeleteCollectionCallBack:)];
    }
}

- (void)getDeleteCollectionCallBack:(NSDictionary *)dic{
    if(dic == nil){
        return;
    }
    if([[Common valueForKey:@"respCode" dic:dic]isEqualToString:@"000"]){
        [[Common shareInstance] showAlertViewWithTitle:nil text:@"已取消收藏" dismissTime:0.8];
        self.isCollected = false;
        UIImageView *img_temp = (UIImageView *)[self.view_bottomBar viewWithTag:300001];
        [img_temp setImage:[UIImage imageNamed:@"Guide/收藏图标.png"]];
    }else{
        [[Common shareInstance] showAlertViewWithTitle:nil text:@"网络超时" dismissTime:0.8];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
